/**
 * $Header: /data/cvs/gsd/src/java/com/gs/common/dao/jdbc/DBConnection.java,v 1.2 2007/06/14 02:18:11 clarkliu Exp $
 * $Revision: 1.2 $
 * $Date: 2007/06/14 02:18:11 $
 *
 * ====================================================================
 *
 * Copyright (c) 2001 Media Data Systems Pte Ltd All Rights Reserved.
 * This software is the confidential and proprietary information of
 * Media Data Systems Pte Ltd.You shall not disclose such Confidential
 * Information.
 *
 * ====================================================================
 *
 */
package io.renren.utils.util.db.connection;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import org.apache.log4j.Logger;

/**
 *
 * @author $Author: clarkliu $
 * @version $Revision: 1.2 $ $Date: 2007/06/14 02:18:11 $
 */
public class DBConnection implements Connection {

	// ----------------------------------------------------- Static Variables
	static Logger logger = Logger.getLogger(DBConnection.class);

	// ----------------------------------------------------- Instance Variables
	private DBConnectionPool pool;

	private Connection conn;

	private boolean inuse;

	private long timestamp;
	
	private String dbName;

	// ----------------------------------------------------- Constructors
	public DBConnection(Connection conn, DBConnectionPool pool) {
		this.conn = conn;
		this.pool = pool;
		this.inuse = false;
		this.timestamp = 0;
	}

	// ----------------------------------------------------- Public methods
	public synchronized boolean lease() {
		if (inuse) {
			return false;
		} else {
			inuse = true;
			timestamp = System.currentTimeMillis();
			return true;
		}
	}

	public boolean validate() {
		try {
			conn.getMetaData();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public boolean inUse() {
		return inuse;
	}

	public long getLastUse() {
		return timestamp;
	}

	public void close() throws SQLException {
//		System.out.println("Connection closed !");
		DBConnectionManager.getInstance().freeConnection(this);
//		pool.returnConnection(this);
	}

	protected void expireLease() {
		inuse = false;
	}

	protected Connection getConnection() {
		return conn;
	}
	
	public String getPoolName() {
		return pool.getPoolName();
	}

	// Code for delegation of java.sql.Connection methods to conn

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setReadOnly(boolean)
	 */
	public void setReadOnly(boolean param1) throws SQLException {
		conn.setReadOnly(param1);
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#isReadOnly()
	 */
	public boolean isReadOnly() throws SQLException {
		return conn.isReadOnly();
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#isClosed()
	 */
	public boolean isClosed() throws SQLException {
		return conn.isClosed();
	}

	/**
	 *
	 * @param param1 <description>
	 * @param param2 <description>
	 * @param param3 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#createStatement(int, int, int)
	 */
	public Statement createStatement(int param1, int param2, int param3) throws SQLException {
		Statement curStatement = conn.createStatement(param1, param2, param3);
		return curStatement;
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#createStatement()
	 */
	public Statement createStatement() throws SQLException {
		Statement curStatement = conn.createStatement();
		return curStatement;
	}

	/**
	 *
	 * @param resultSetType <description>
	 * @param param2 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#createStatement(int, int)
	 */
	public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
		Statement curStatement = conn.createStatement(resultSetType, resultSetConcurrency);
		return curStatement;
	}

	/**
	 *
	 * @param sql <description>
	 * @param param2 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareStatement(String, int)
	 */
	public PreparedStatement prepareStatement(String sql, int param2) throws SQLException {
		PreparedStatement curStatement = conn.prepareStatement(sql, param2);
		return curStatement;
	}

	/**
	 *
	 * @param sql <description>
	 * @param param2 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareStatement(String, String[])
	 */
	public PreparedStatement prepareStatement(String sql, String[] param2) throws SQLException {
		PreparedStatement curStatement = conn.prepareStatement(sql, param2);
		return curStatement;
	}

	/**
	 *
	 * @param sql <description>
	 * @param param2 <description>
	 * @param param3 <description>
	 * @param param4 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareStatement(String, int, int, int)
	 */
	public PreparedStatement prepareStatement(String sql, int param2, int param3, int param4) throws SQLException {
		PreparedStatement curStatement = conn.prepareStatement(sql, param2, param3, param4);
		return curStatement;
	}

	/**
	 *
	 * @param sql <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareStatement(String)
	 */
	public PreparedStatement prepareStatement(String sql) throws SQLException {
		PreparedStatement curStatement = conn.prepareStatement(sql);
		return curStatement;
	}

	/**
	 *
	 * @param sql <description>
	 * @param param2 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareStatement(String, int[])
	 */
	public PreparedStatement prepareStatement(String sql, int[] param2) throws SQLException {
		PreparedStatement curStatement = conn.prepareStatement(sql, param2);
		return curStatement;
	}

	/**
	 *
	 * @param sql <description>
	 * @param param2 <description>
	 * @param param3 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareStatement(String, int, int)
	 */
	public PreparedStatement prepareStatement(String sql, int param2, int param3) throws SQLException {
		PreparedStatement curStatement = conn.prepareStatement(sql, param2, param3);
		return curStatement;
	}

	/**
	 *
	 * @param param1 <description>
	 * @param param2 <description>
	 * @param param3 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareCall(String, int, int)
	 */
	public CallableStatement prepareCall(String param1, int param2, int param3) throws SQLException {
		return conn.prepareCall(param1, param2, param3);
	}

	/**
	 *
	 * @param param1 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareCall(String)
	 */
	public CallableStatement prepareCall(String param1) throws SQLException {
		return conn.prepareCall(param1);
	}

	/**
	 *
	 * @param param1 <description>
	 * @param param2 <description>
	 * @param param3 <description>
	 * @param param4 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#prepareCall(String, int, int, int)
	 */
	public CallableStatement prepareCall(String param1, int param2, int param3, int param4) throws SQLException {
		return conn.prepareCall(param1, param2, param3, param4);
	}

	/**
	 *
	 * @param param1 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#nativeSQL(String)
	 */
	public String nativeSQL(String param1) throws SQLException {
		return conn.nativeSQL(param1);
	}

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setAutoCommit(boolean)
	 */
	public void setAutoCommit(boolean param1) throws SQLException {
		conn.setAutoCommit(param1);
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getAutoCommit()
	 */
	public boolean getAutoCommit() throws SQLException {
		return conn.getAutoCommit();
	}

	/**
	 *
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#commit()
	 */
	public void commit() throws SQLException {

		conn.commit();

	}

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#rollback(Savepoint)
	 */
	public void rollback(Savepoint param1) throws SQLException {
		conn.rollback(param1);

	}

	/**
	 *
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#rollback()
	 */
	public void rollback() throws SQLException {

		conn.rollback();

	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getMetaData()
	 */
	public DatabaseMetaData getMetaData() throws SQLException {
		return conn.getMetaData();
	}

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setCatalog(String)
	 */
	public void setCatalog(String param1) throws SQLException {
		conn.setCatalog(param1);
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getCatalog()
	 */
	public String getCatalog() throws SQLException {
		return conn.getCatalog();
	}

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setTransactionIsolation(int)
	 */
	public void setTransactionIsolation(int param1) throws SQLException {
		conn.setTransactionIsolation(param1);
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getTransactionIsolation()
	 */
	public int getTransactionIsolation() throws SQLException {
		return conn.getTransactionIsolation();
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getWarnings()
	 */
	public SQLWarning getWarnings() throws SQLException {
		return conn.getWarnings();
	}

	/**
	 *
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#clearWarnings()
	 */
	public void clearWarnings() throws SQLException {
		conn.clearWarnings();
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getTypeMap()
	 */
	public Map getTypeMap() throws SQLException {
		return conn.getTypeMap();
	}
//
//	/**
//	 *
//	 * @param param1 <description>
//	 * @exception java.sql.SQLException <description>
//	 * @see java.sql.Connection#setTypeMap(Map)
//	 */
//	public void setTypeMap(Map param1) throws SQLException {
//		conn.setTypeMap(param1);
//	}

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setHoldability(int)
	 */
	public void setHoldability(int param1) throws SQLException {
		conn.setHoldability(param1);
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#getHoldability()
	 */
	public int getHoldability() throws SQLException {
		return conn.getHoldability();
	}

	/**
	 *
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setSavepoint()
	 */
	public Savepoint setSavepoint() throws SQLException {
		return conn.setSavepoint();
	}

	/**
	 *
	 * @param param1 <description>
	 * @return <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#setSavepoint(String)
	 */
	public Savepoint setSavepoint(String param1) throws SQLException {
		return conn.setSavepoint(param1);
	}

	/**
	 *
	 * @param param1 <description>
	 * @exception java.sql.SQLException <description>
	 * @see java.sql.Connection#releaseSavepoint(Savepoint)
	 */
	public void releaseSavepoint(Savepoint param1) throws SQLException {
		conn.releaseSavepoint(param1);
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		// TODO Auto-generated method stub
		conn.setTypeMap(map);
	}

	@Override
	public Clob createClob() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Blob createBlob() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public NClob createNClob() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SQLXML createSQLXML() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isValid(int timeout) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getClientInfo(String name) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Properties getClientInfo() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}


	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}


	public void setSchema(String schema) throws SQLException {
		// TODO Auto-generated method stub
		
	}


	public String getSchema() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}


	public void abort(Executor executor) throws SQLException {
		// TODO Auto-generated method stub
		
	}


	public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
		// TODO Auto-generated method stub
		
	}


	public int getNetworkTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

}
